var accCode = '';
var accNum = '';
var arrAccCode = new Array();
var arrAccNo = new Array();
Ext.define('HIT.controller.MdataAccountCenter', {
	extend: 'Ext.app.Controller',
	models:['MdataAccountModel'],
	stores:['MdataAccountStore'],
	views: ['MdataAccountPanel','MdataAccountAddPanel','MdataAccountEditPanel'],
	
	
	onButtonClick: function(button, e, options) {
		if (button.id == 'mdata-account-add') {   //增加
			add = Share.createPanelByAlias('widget.mdataaccountaddpanel');
            Share.showPanelAsWindow(add);
            arrAccCode = [];
            arrAccNo = [];
		}else if(button.id == 'mdata-account-edit'){  //修改	
			var check = Share.checkFun();
			if (check) {
				var tabpanelId = 'tab-mdata-cancelaccount';          
	            var tab = Ext.getCmp(tabpanelId);
	            if (tab == undefined) {
	                return;
	            }
	
	            if (tab.getSelectionModel().getCount() == 0) {
	            	Share.ShowMessage("I0011","一条记录");
	                return;
	            }
	            if (tab.getSelectionModel().getCount() != 1) {
	            	Share.ShowMessage("E0039","记录");
	                return;
	            }
	            
	            var row = tab.getSelectionModel().getSelection()[0];
	            accCode = row.get('pin_account_code');
	            accNum = row.get('pin_account');
				edit = Share.createPanelByAlias('widget.mdataaccounteditpanel');
				Ext.getCmp('hiddenID').setValue(row.get('id'));
				var fix = Ext.getCmp('tab-mdata-cancelaccount').getSelectionModel().getSelection()[0];
			    Ext.getCmp('editmdataaccountcode').setValue(fix.get('pin_account_code'));
		        Ext.getCmp('editmdataaccount').setValue(fix.get('pin_account'));
	            Share.showPanelAsWindow(edit);
	            arrAccCode = [];
	            arrAccNo = [];
			}else {
				Share.ShowMessage("E0026");
				return;
			}
		}else if(button.id == 'mdata-account-delete'){  //消方银行账号删除	
			var tabpanelId = 'tab-mdata-cancelaccount';          
            var tab = Ext.getCmp(tabpanelId);
            if (tab == undefined) {
                return;
            }

            if (tab.getSelectionModel().getCount() == 0) {
            	Share.ShowMessage("I0008");
                return;
            }
//            if (tab.getSelectionModel().getCount() != 1) {
//                Ext.MessageBox.alert(Share.T('一次只能选择一条记录!'));
//                return;
//            }
            
            var row = tab.getSelectionModel().getSelection();
            
            var Id_Arr = "(";
            for(var ri_d = 0;ri_d < row.length;ri_d++){
            	Id_Arr += "'" + row[ri_d].get('id') + "'";
            	if(ri_d < row.length - 1){
            		Id_Arr += ",";
            	}
            }
            Id_Arr += ")";
            
            Ext.MessageBox.confirm(Share.T('提示'),Share.T('确定删除吗？'),function(btn){
            	if(btn == 'yes'){ 
            		var check = Share.checkFun();
        			if (check) {
            		Share.AjaxRequest({
                        url: ctx + "/deletemdataaccount",
                        params: {
                            id: Id_Arr
                        },
                        showMsg: false,
                        scope: this,
                        callback: function (json) {
                            tab.getStore().reload();
                            if(tab.getStore().getCount() == 1){
                            	tab.getStore().removeAll();
                            	Ext.getCmp('mdata-account-pag').movePrevious();
                    		}
                            Share.ShowMessage("I0006","删除");
                        },
                        falseFun: function (json) { //失败后想做的个性化操作函数
                       	 Ext.MessageBox.alert(json.msg);	
                       }
                    }); 
            	}else {
    				Share.ShowMessage("E0026");
    				return;
    			 }
            	}
            });
		}
	},
	save: function (win) {  //保存(添加/修改)
   	 	var tabpanelId = 'tab-mdata-cancelaccount';          
   	 	var tab = Ext.getCmp(tabpanelId);
        if (tab == undefined) {
            return;
        }
        var functionId = Ext.getCmp('mdata-functionId').value;
   	
        if (functionId == 1){  //添加-保存
        	var mdataaccountcode = Ext.getCmp('addmdataaccountcode').getValue();
        	var mdataaccount = Ext.getCmp('addmdataaccount').getValue();
        	if(mdataaccountcode.trim().length == 0 || mdataaccountcode.trim().length > 20){
        		Ext.getCmp('addmdataaccountcode').markInvalid(Share.T('销方银行账号代码不能为空'));
        		return;
        	}if(mdataaccount.trim().length == 0 || mdataaccount.trim().length > 20){
        		Ext.getCmp('addmdataaccount').markInvalid(Share.T('销方银行账号不能为空'));
        		return;
        	}
        	
        	Ext.Ajax.request({
                url: ctx + "/getMdataAcc",
                params: {      	                                         
                },
                async:false,
                success : function(response, options){
                    var acctext = response.responseText;
                	if(acctext != '' && acctext != null && acctext != undefined){
                	var json = Ext.decode(response.responseText);
	                    for(var i = 0; i < json.length; i++ ){
	                    	arrAccCode[i] = json[i].SELLER_CODE;
	                    	arrAccNo[i] = json[i].BANK_ACCOUNT;
	                    }
                	}
                }
            });
        	for(var i = 0; i < arrAccCode.length; i++){
        		if(mdataaccountcode.toUpperCase() == arrAccCode[i].toUpperCase()){
        			Share.ShowMessage("I0013","相同的销方银行账号代码");
        			return;
        		}
        	}
        	Share.AjaxRequest({
                url: ctx + "/addmdataaccount",
                params: {
                	mdataaccountcode: mdataaccountcode,
                	mdataaccount: mdataaccount             	                                         
                },
                showMsg: false,
                callback: function (json) {
                	Share.ShowMessage("I0006","添加");
                    tab.getStore().reload();
                    win.close();         	
                }
            });
        }else if (functionId == 2){  //修改-保存
        	var mdataaccountcode = Ext.getCmp('editmdataaccountcode').getValue();
        	var mdataaccount = Ext.getCmp('editmdataaccount').getValue();
        	var id = Ext.getCmp('hiddenID').value;
        	if(mdataaccountcode.trim().length == 0 || mdataaccountcode.trim().length > 20){
        		Ext.getCmp('editmdataaccountcode').markInvalid(Share.T('销方银行账号代码不能为空'));
        		return;
        	}if(mdataaccount.trim().length == 0 || mdataaccount.trim().length > 20){
        		Ext.getCmp('editmdataaccount').markInvalid(Share.T('销方银行账号不能为空'));
        		return;
        	}
        	Ext.Ajax.request({
                url: ctx + "/getMdataAcc",
                params: {      	                                         
                },
                async:false,
                success : function(response, options){
                    var acctext = response.responseText;
                	if(acctext != '' && acctext != null && acctext != undefined){
                	var json = Ext.decode(response.responseText);
	                    for(var i = 0; i < json.length; i++ ){
	                    	arrAccCode[i] = json[i].SELLER_CODE;
	                    	arrAccNo[i] = json[i].BANK_ACCOUNT;
	                    }
                	}
                }
            });
        	
        	if(mdataaccountcode.toUpperCase() != accCode.toUpperCase()){
        		for(var i=0;i<arrAccCode.length;i++){
            		if(mdataaccountcode.toUpperCase() == arrAccCode[i].toUpperCase() &&
            				arrAccCode[i].toUpperCase() != accCode.toUpperCase()	
            		){
            			Share.ShowMessage("I0013","相同的销方银行账号代码");
            			return;
            		}
            	}
        	}
        	
        	Share.AjaxRequest({
                url: ctx + "/updatemdataaccount",
                params: {
                	mdataaccountcode: mdataaccountcode,
                	mdataaccount: mdataaccount,         
                	id: id
                },
                showMsg: false,
                callback: function (json) {
                	Share.ShowMessage("I0006","修改");
                    tab.getStore().reload();
                    win.close();         	
                }
            });
        }
	},
	
	init: function(application) {
        this.control({
            "button": {
                click: this.onButtonClick
            }
        });
    }
});